package jdbc;

import javax.security.sasl.SaslClient;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.Scanner;

public class UPdateUserDemo {
    public static void main(String[] args) {
        try {
            //用户修改数据
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = sc.next();
            System.out.println("请输入新密码：");
            String password = sc.next();
            System.out.println("请输入新昵称：");
            String nickname = sc.next();
            //数据库链接
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root", "root");
            Statement statement = connection.createStatement();
            //录入数据库
            String sql = "update userinfo set password = '" + password + "',nickname = '" + nickname + "' where username = '" + username + "'";
            int num = statement.executeUpdate(sql);
            if (num == 0) {
                System.out.println("用户不存在！");
            } else {
                System.out.println("修改成功！");
            }
            connection.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
